package com.harmony.pilot.data.controller;

import com.harmony.pilot.common.dto.CodeContext;
import com.harmony.pilot.common.dto.RestResult;
import com.harmony.pilot.data.service.OpenHarmonyAnalyzer;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * OpenHarmony专用API控制器
 */
@Slf4j
@RestController
@RequestMapping("/data/openharmony")
@RequiredArgsConstructor
public class OpenHarmonyController {
    
    private final OpenHarmonyAnalyzer openHarmonyAnalyzer;
    
    /**
     * 分析OpenHarmony项目
     */
    @GetMapping("/analyze-project")
    public RestResult<Map<String, Object>> analyzeProject(@RequestParam String projectRoot) {
        log.info("Analyzing OpenHarmony project: {}", projectRoot);
        
        try {
            Map<String, Object> analysis = openHarmonyAnalyzer.analyzeOHProject(projectRoot);
            return RestResult.success(analysis);
        } catch (Exception e) {
            log.error("Failed to analyze project", e);
            return RestResult.error("Failed to analyze project: " + e.getMessage());
        }
    }
    
    /**
     * 查找相关的OpenHarmony API
     */
    @PostMapping("/find-apis")
    public RestResult<List<String>> findRelatedAPIs(@RequestBody CodeContext context) {
        log.info("Finding related OpenHarmony APIs");
        
        try {
            List<String> apis = openHarmonyAnalyzer.findRelatedOHAPIs(context);
            return RestResult.success(apis);
        } catch (Exception e) {
            log.error("Failed to find APIs", e);
            return RestResult.error("Failed to find APIs: " + e.getMessage());
        }
    }
    
    /**
     * 检测是否为OpenHarmony代码
     */
    @PostMapping("/detect")
    public RestResult<Boolean> detectOpenHarmonyCode(@RequestBody CodeContext context) {
        log.info("Detecting OpenHarmony code");
        
        try {
            boolean isOH = openHarmonyAnalyzer.isOpenHarmonyCode(
                context.getFilePath(), 
                context.getCode()
            );
            return RestResult.success(isOH);
        } catch (Exception e) {
            log.error("Failed to detect", e);
            return RestResult.error("Failed to detect: " + e.getMessage());
        }
    }
    
    /**
     * 获取OpenHarmony编码规范
     */
    @GetMapping("/coding-guidelines")
    public RestResult<List<String>> getCodingGuidelines() {
        log.info("Getting OpenHarmony coding guidelines");
        
        try {
            List<String> guidelines = openHarmonyAnalyzer.getCodingGuidelines();
            return RestResult.success(guidelines);
        } catch (Exception e) {
            log.error("Failed to get guidelines", e);
            return RestResult.error("Failed to get guidelines: " + e.getMessage());
        }
    }
    
    /**
     * 分析文件管理子系统
     */
    @GetMapping("/analyze-filemanagement")
    public RestResult<Map<String, Object>> analyzeFileManagement(@RequestParam String projectRoot) {
        log.info("Analyzing File Management subsystem");
        
        try {
            Map<String, Object> analysis = openHarmonyAnalyzer.analyzeFileManagementSubsystem(projectRoot);
            return RestResult.success(analysis);
        } catch (Exception e) {
            log.error("Failed to analyze File Management", e);
            return RestResult.error("Failed to analyze: " + e.getMessage());
        }
    }
    
    /**
     * 分析图形2D子系统
     */
    @GetMapping("/analyze-graphic2d")
    public RestResult<Map<String, Object>> analyzeGraphic2D(@RequestParam String projectRoot) {
        log.info("Analyzing Graphic 2D subsystem");
        
        try {
            Map<String, Object> analysis = openHarmonyAnalyzer.analyzeGraphic2DSubsystem(projectRoot);
            return RestResult.success(analysis);
        } catch (Exception e) {
            log.error("Failed to analyze Graphic 2D", e);
            return RestResult.error("Failed to analyze: " + e.getMessage());
        }
    }
}

